//Erstellt von Moritz Liepe
package main.editor;

import java.util.UUID;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;


public class ImportHandler extends AbstractHandler 
{

	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException 
	{
		FileDialog fileDialog = new FileDialog(HandlerUtil.getActiveWorkbenchWindow(arg0).getShell(), SWT.OPEN);
		fileDialog.setFilterPath("C:/");
		String[] extensions = { "*.xml", "*.*" };
		fileDialog.setFilterExtensions(extensions);
		String fileName = fileDialog.open();
	    if (fileName != null) 
	    {
	    	IWorkbenchPart part = HandlerUtil.getActivePart(arg0);
	    	if(part instanceof VOSEditor )
	    	{
	    		((VOSEditor )part).refreshSelector(fileName);
	    	}
	    	else {
	    		VOSEditorInput vosEditorInput = new VOSEditorInput(UUID.randomUUID().toString());
				try 
				{
					HandlerUtil.getActiveWorkbenchWindow(arg0).getActivePage().openEditor(vosEditorInput, VOSEditor.class.getCanonicalName());
					part = HandlerUtil.getActivePart(arg0);
			    	if(part instanceof VOSEditor )
			    		((VOSEditor )part).refreshSelector(fileName);
				}
				catch (PartInitException e) 
				{
					// TODO Message
					e.printStackTrace();
				}
	    	}
	    }
	    
		return null;
	
	}

}
